package com.party.web.biz.circle;

import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.circle.Circle;
import com.party.core.model.circle.CircleMember;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.circle.ICircleMemberService;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.circle.biz.CircleBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.web.biz.file.FileBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 圈子业务处理
 *
 * @author yifeng
 * @date 2018/8/31 0031
 * @time 上午 10:38
 */
@Service
public class CircleBizNewService {
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private CircleBizService circleBizService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private ICircleService circleService;
    @Autowired
    private ICircleMemberService circleMemberService;

    Logger logger = LoggerFactory.getLogger(getClass());

    public void uploadQrCode(String memberId, String circleId) {
        try {
            String filePath = fileBizService.getUploadCIPath(memberId, "circle");
            String qrContent = "circle/circle_detail.html?id=" + circleId;
            fileBizService.uploadCloudOut(circleId, filePath, qrContent, memberId);
            //上传通讯录二维码
            String fileContactPath = fileBizService.getUploadCIPath(memberId, "circle/contact");
            String contactQrContent = "circle/circle_contact.html?id="+circleId;
            fileBizService.uploadCloudOut(circleId, fileContactPath, contactQrContent, memberId);
        } catch (Exception e) {
            logger.error("生成圈子二维码异常", e);
        }
    }

    public String getReturnUrl(String memberId) {
        if (StringUtils.isNotEmpty(memberId)) {
            List<Circle> circleList = circleBizService.joinList(new Circle(), null, memberId, null);
            if (circleList.size() == 1) {
                return "circle/circle_detail.html?id=" + circleList.get(0).getId() + "&r=" + System.currentTimeMillis();
            }
        }
        return "circle/my_circle_list.html?r=" + System.currentTimeMillis();
    }

    /**
     * 行知公社圈子数据迁移到子账号下
     *
     * @param title        查询的标题
     * @param xzgsMemberId 行知公社用户id
     * @param username     子账号的用户名
     * @author yifeng
     * @date 14:54 2019/11/12
     **/
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void xzgsCircleDataMigration(String title, String xzgsMemberId, String username) {
        String memberId = "";
        List<String> circleIds = circleService.getXzgsCircleList(title, xzgsMemberId);
        if (circleIds.size() > 0) {
            MemberGroup memberGroup = memberGroupService.findByUserName(username);
            memberId = memberGroup.getId();
        }

        for (String circleId : circleIds) {
            this.addCreatorMember(circleId, memberId, xzgsMemberId);
            this.uploadQrCode(memberId, circleId);
        }
    }

    /**
     * 把迁移后的用户添加为管理员，移除原来的管理员
     *
     * @param circleId     圈子id
     * @param memberId     新的用户id
     * @param xzgsMemberId 行知公社用户id
     * @author yifeng
     * @date 14:44 2019/11/12
     **/
    public void addCreatorMember(String circleId, String memberId, String xzgsMemberId) {
        CircleMember newCircleMember = circleMemberService.getUnique(new CircleMember(circleId, memberId));
        if (newCircleMember == null) {
            CircleMember circleMember = new CircleMember();
            circleMember.setCircle(circleId);
            circleMember.setMember(memberId);
            circleMember.setCreateBy(memberId);
            circleMember.setUpdateBy(memberId);
            circleMember.setIsAdmin(YesNoStatus.YES.getCode());
            circleMemberService.insert(circleMember);
        }

        CircleMember oldCircleMember = circleMemberService.getUnique(new CircleMember(circleId, xzgsMemberId));
        if (null != oldCircleMember) {
            oldCircleMember.setIsAdmin(YesNoStatus.NO.getCode());
            circleMemberService.update(oldCircleMember);
        }

        Circle circle = circleService.get(circleId);
        circle.setCreateBy(memberId);
        circle.setMemberNum(circle.getMemberNum() + 1);
        circleService.update(circle);
    }
}
